# cmakelist.txt

# cmake最低版本号设置--此为必填项
cmake_minimum_required (VERSION 2.8)

# 设置cmake的工程名
set(project_name "cmake-test")
project(${project_name})


if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64") #如果ubuntu是32位的，这里换成x86_32即可
# 设置编译器优化选项
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -g -ggdb -O0 ")
SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3 -Wall -Wno-builtin-macro-redafined -s")

SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g -ggdb -O0 -std=gnu++11")
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3 -Wall -Wno-builtin-macro-redafined -s -std=gnu++11")

# 设置工程所需的头文件路径
include_directories (
    "/usr/include"
    "/usr/local/include"

)

# 设置工程所需的库路径
link_directories("/usr/lib")
link_directories("/usr/local/lib")

endif()


# 设置库的名称
set(project_lib "mysqrt")
set(project_static_lib "mysqrt_s")

# 设置工程所需的头文件路径
include_directories (
    "./include"
)

# build lib
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)  # 设置库的生成目录
aux_source_directory(${PROJECT_SOURCE_DIR}/src LIB_SRCS)  #将src目录下的文件放在LIB_SRCS集合

if(${TARGET_LIB} MATCHES "share")  # 自定义一个TARGET_LIB名称，用来判断需要生成库的类型
add_library(${project_lib} SHARED ${LIB_SRCS})                    # 将src目录下的文件编译成动态库
target_link_libraries(${project_lib} m)   # 因为test.c中使用了数学函数`sqrt`，所以在编译动态库时，需要链接libm.so
set_target_properties(${project_lib} PROPERTIES VERSION 1.0.0)    # 设置动态库的版本号
set_target_properties(${project_lib} PROPERTIES OUTPUT_NAME ${project_lib})  #设置动态库的名称
set_target_properties(${project_lib} PROPERTIES CLEAN_DIRECT_OUTPUT 1)   #清空输出，为了方便同时生成静态库
else()    # else 语句
add_library(${project_static_lib} STATIC ${LIB_SRCS})  # 生成静态库
set_target_properties(${project_static_lib} PROPERTIES OUTPUT_NAME ${project_lib})  #设置静态库的名称
target_link_libraries(${project_static_lib} m)   # 因为test.c中使用了数学函数`sqrt`，所以在编译动态库时，需要链接libm.so
set_target_properties(${project_static_lib} PROPERTIES CLEAN_DIRECT_OUTPUT 1)
endif()   # 结束判断if语句

# 设置工程所需的库路径
link_directories("${LIBRARY_OUTPUT_PATH}")  # 此内容与link_directories("${PROJECT_SOURCE_DIR}/lib")一致，都为上一步生成的

# 在上述设置的库路径中，查找所需的库
set(project_lib
    ${project_lib}
)

# 将src下的文件包含在SRCS的集合内
# aux_source_directory(${PROJECT_SOURCE_DIR}/src SRCS)  # 因为src目录下的文件已经被编译成库，所以这里不需要再引用src下的文件，用`#`号屏蔽

# 我们在此也设置一下最后生成的可执行程序输出目录
set(EXECUTABLE_OUTPUT_PATH  ${PROJECT_SOURCE_DIR}/bin)  # 既然区分了编译器的debug与release，所以这里也同步进行最后可执行程序的区分
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/debug_bin) 
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/release_bin) 


# 生成目标可执行文件的名称
add_executable(${project_name} main.c ${SRCS})
# 链接可执行程序所需的库
target_link_libraries(${project_name} ${project_lib})
